package com.strawberry.chat2.library.common;

import android.content.SharedPreferences;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

public class StoregeModule extends ReactContextBaseJavaModule {
    ReactApplicationContext context = null;

    public StoregeModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.context = reactContext;
    }

    @Override
    public String getName() {
        return "H5Storage";
    }

    @ReactMethod
    public void setItem(String key, String value){
        SharedPreferences neteaseSettings = context.getSharedPreferences("common", 0);
        SharedPreferences.Editor editor = neteaseSettings.edit();
        editor.putString(key,value);
        editor.commit();
    }

    @ReactMethod
    public void getIten(String key, Callback callback){
        String value = getItenAsync(key);
        callback.invoke(value);
    }

    @ReactMethod
    public void removeItem(String key){
        SharedPreferences neteaseSettings = context.getSharedPreferences("common", 0);
        SharedPreferences.Editor editor = neteaseSettings.edit();
        editor.remove(key);
        editor.commit();
    }

    public String getItenAsync(String key){
        SharedPreferences neteaseSettings = context.getSharedPreferences("common", 0);
        String value = neteaseSettings.getString(key, "");
        return value;
    }
}
